package com.bidu.common;

import org.apache.logging.log4j.ThreadContext;

import java.util.concurrent.ThreadLocalRandom;

public class ThreadTokenHelper {

    public ThreadTokenHelper() {
    }

    private static String genThreadToken() {
        long head = System.currentTimeMillis() % 86400000L;
        long body = Thread.currentThread().getId();
        int foot = ThreadLocalRandom.current().nextInt(1000);
        String token = head + "_" + body + "_" + foot;
        ThreadContext.put("token", token);
        return token;
    }

    public static String getThreadToken() {
        String token = ThreadContext.get("token");
        return token == null ? genThreadToken() : token;
    }

    public static void clear() {
        ThreadContext.remove("token");
    }
}
